/**
 * Project Name:MyTookit
 * Package Name:com.xuwei.utils.db
 * author:David
 * Date:2016年6月2日上午9:13:08
 * Copyright (c) 2016, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.utils.db;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.LinkedList;
import java.util.Properties;

/**
 * @author David
 * @since 2016年6月2日 上午9:13:08
 * @version 
 * @since JDK 1.6
 */
public class MyDbPool {

	private static String driverClassName;
	private static String url;
	private static String user;
	private static String password;
	private static LinkedList<Connection> pool = new LinkedList<Connection>();//存储连接的池
	static{
		try {
			InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dbinfo.properties");
			Properties props = new Properties();
			props.load(in);
			driverClassName = props.getProperty("className");
			url = props.getProperty("url");
			user = props.getProperty("user");
			password = props.getProperty("password");
			Class.forName(driverClassName);
			
			//初始化10个连接到池中
			for(int i=0;i<10;i++){
				Connection conn = DriverManager.getConnection(url, user, password);
				pool.add(conn);
			}
//			System.out.println("初始化的连接有：");
//			for(Connection c:pool)
//				System.out.println(c);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public synchronized static Connection getConnection(){
		if(pool.size()>0){
			Connection conn = pool.remove();//从池中拿到连接，并从池中删除掉
			
//			System.out.println("取出的是："+conn);
//			System.out.println("池中还有：");
//			for(Connection c:pool)
//				System.out.println(c);
			return conn;
		}else{
			return null;
		}
	}
	public static void release(Connection conn){//把连接放回池中
		pool.add(conn);
//		System.out.println("回来的是：" +conn);
//		System.out.println("回来后池中的连接：");
//		for(Connection c:pool)
//			System.out.println(c);
	}
}
